/**
 * @param {number[]} nums
 * @return {number[]}
 */
export var nextGreaterElements = function (nums) {
  const [n, stack, ans] = [nums.length, [], new Array(nums.length).fill(-1)]
  for (let i = 0; i < 2 * n; i++) {
    const num = nums[i % n]
    while (stack.length && nums[stack[stack.length - 1] % n] < num) {
      const popIndex = stack.pop()
      if (popIndex < n) ans[popIndex] = num
    }
    stack.push(i)
  }
  return ans
}
